package com.isias.system.Controller;

import com.isias.common.result.Result;
import com.isias.model.system.SysMenu;
import com.isias.model.vo.AssginMenuVo;
import com.isias.system.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author fanyi
 * @since 2023-02-21
 */
@RestController
@RequestMapping("/admin/system/sysMenu")
public class SysMenuController {

    @Autowired
    private SysMenuService service;


    /**
     * 更改用户状态
     */
    @GetMapping("updataStatus/{id}/{status}")
    public Result updataStatus(@PathVariable String id,@PathVariable Integer status){
        if (service.updataStatus(id,status)) {
            return Result.ok(true);
        }else {
            return Result.fail(false);
        }
    }
    /***
     * 根据角色获取菜单（树形结构）
     */
    @GetMapping("/toAssign/{roleId}")
    public Result toAssign(@PathVariable String roleId) {
        List<SysMenu> list = service.findSysMenuByRoleId(roleId);
        if (list!=null){
            return Result.ok(list);
        }
        return Result.fail(list);
    }
    /***
     * 给角色分配菜单权限
     */
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginMenuVo assginMenuVo){
        if (service.doAssign(assginMenuVo)){
            return Result.ok();
        }
        return Result.fail();
    }

    /***
     * 获取菜单（树形结构）
     */
    @GetMapping
    public Result SelectAllTree(){
        List<SysMenu> list = service.findNodes();
        if (list!=null){
            return Result.ok(list);
        }
        return Result.fail();
    }

    /***
     * 增加
     */
    @PostMapping
    public Result AddMenu(@RequestBody SysMenu sysMenu){
        if (service.save(sysMenu)) {
            return Result.ok();
        }
        return Result.fail();
    }
    /***
     * 修改
     */
    @PutMapping
    public Result UpdataMenu(@RequestBody SysMenu sysMenu){
        if (service.updateById(sysMenu)) {
            return Result.ok();
        }
        return Result.fail();
    }
    /***
     * 删除
     */
    @DeleteMapping("{id}")
    public Result DeleteMenu(@PathVariable String id){
        if (service.removeMenuById(id)) {
            return Result.ok();
        }
        return Result.fail();
    }
    /***
     * 根据id查询
     */
    @GetMapping("{id}")
    public Result SelectById(@PathVariable String id){
        SysMenu menu = service.getById(id);
        if (menu!=null){
            return Result.ok(menu);
        }
        return Result.fail(null);
    }




}

